class Board(object):
    def __init__(self,height,width):
        self._height = height
        self._width = width
        self._slots = [[' '] * self._width for row in range(self._height)]

    def __repr__(self):
        """ Returns a string representation for a Board object.
            """
        s = ''  # begin with an empty string
        # add one row of slots at a time
        for row in range(self._height):
            s += '|'  # one vertical bar at the start of the row
            for col in range(self._width):
                s += self._slots[row][col] + '|'
            s += '\n'  # newline at the end of the row

        # Add code here for the hyphens at the bottom of the board
        # and the numbers underneath it.
        s += '--'*self._width
        s += '-\n'
        for col_num in range(self._width):
            s += ' '
            s += str(int(col_num % 10))
        return s

    def add_checker(self,checker,col):
        """ put your docstring here
            """
        assert (checker == 'X' or checker == 'O')
        assert (0 <= col < self._width)

        # put the rest of the method here
        cur_row = self._height-1
        while self._slots[cur_row][col] != ' ':
            cur_row -= 1
        self._slots[cur_row][col] = checker
        print(self)

    def add_checkers(self, colnums):
        """ takes in a string of column numbers and places alternating
            checkers in those columns of the called Board object,
            starting with 'X'.
        """
        checker = 'X'  # start by playing 'X'

        for col_str in colnums:
            col = int(col_str)
            if 0 <= col < self._width:
                self.add_checker(checker, col)

            # switch to the other checker
            if checker == 'X':
                checker = 'O'
            else:
                checker = 'X'

    def reset(self):
        '''reset the Board object on which it is called
        by setting all slots to contain a space character
        :return:
        '''
        self._slots = [[' '] * self._width for row in range(self._height)]
        print(self)

    def can_add_to(self,col):
        '''returns True if it is valid to place a checker in the column col on the calling Board object. Otherwise, it should return False
        '''
        if col<0 or col>=self._width :
            return False
        if self._slots[0][col] == ' ':
            return True
        return  False

    def is_full(self):
        '''
        returns True if the called Board object is completely full of checkers,
        and returns False otherwise
        :return:
        '''
        for row in range(self._height):
            for col in range(self._width):
                if self._slots[row][col] == ' ':
                    return False
        return True

    def remove_checker(self,col):
        '''
        removes the top checker from column col of the called Board object.
         If the column is empty, then the method should do nothing.
        :param col:
        :return:
        '''
        if col<0 or col>=self._width :
            return False
        for row in range(self._height):
            if self._slots[row][col] != ' ':
                self._slots[row][col] = ' '
                break
        #print(self)

    def is_win_for(self,checker):

        """ put your docstring here
        """
        assert(checker == 'X' or checker == 'O')
        return self.is_horizontal_win(checker) or\
            self.is_vertical_win(checker) or \
            self.is_down_diagonal_win(checker) or \
            self.is_up_diagonal_win(checker)
        # call the helper functions and use their return values to
        # determine whether to return True or False


    def is_horizontal_win(self, checker):
        """ Checks for a horizontal win for the specified checker.
            """
        for row in range(self._height):
            for col in range(self._width - 3):
                # Check if the next four columns in this row
                # contain the specified checker.
                if self._slots[row][col] == checker and \
                        self._slots[row][col + 1] == checker and \
                        self._slots[row][col + 2] == checker and \
                        self._slots[row][col + 3] == checker:
                    return True
        # if we make it here, there were no horizontal wins
        return False

    def is_vertical_win(self,checker):
        for col in range(self._width):
            for row in range(self._height -3):
                if self._slots[row][col] == checker and \
                        self._slots[row + 1][col] == checker and \
                        self._slots[row + 2][col] == checker and \
                        self._slots[row + 3][col] == checker:
                    return True
                # if we make it here, there were no horizontal wins
        return False

    def is_down_diagonal_win(self,checker):
        for row in range(self._height -3):
            for col in range(self._width - 3):
                # Check if the next four columns in this row
                # contain the specified checker.
                if self._slots[row][col] == checker and \
                        self._slots[row + 1][col + 1] == checker and \
                        self._slots[row + 2][col + 2] == checker and \
                        self._slots[row + 3][col + 3] == checker:
                    return True
        # if we make it here, there were no horizontal wins
        return False

    def is_up_diagonal_win(self,checker):
        for row in range(self._height-1,2,-1):
            for col in range(self._width - 3):
                # Check if the next four columns in this row
                # contain the specified checker.
                if self._slots[row][col] == checker and \
                        self._slots[row - 1][col + 1] == checker and \
                        self._slots[row - 2][col + 2] == checker and \
                        self._slots[row - 3][col + 3] == checker:
                    return True
        # if we make it here, there were no horizontal wins
        return False

if __name__=='__main__':
    b = Board(6, 7)
    b.add_checkers('23242525515')
    print(b.is_win_for('X'))
    #print(b)
